package com.tunan.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ClassName: Obj2Map
 * @Description:将实体BEAN转成MAP
 * @author: kinda
 * @date: 2016年3月22日 下午4:57:34
 */
public class Obj2Map {

	public static Map getValue(Object thisObj) {
		Map subMap = new HashMap();
		Class c = null;
		try {
			c = Class.forName(thisObj.getClass().getName());
			Method[] m = c.getMethods();
			for (int i = 0; i < m.length; i++) {
				String method = m[i].getName();
				if (method.startsWith("get")) {
					try {
						Object value = m[i].invoke(thisObj);
						if (value != null) {
							String key = method.substring(3);
							key = key.substring(0, 1).toUpperCase() + key.substring(1);
							subMap.put(key, value);
						}
					} catch (Exception e) {
						System.out.println("error:" + method);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Map> map = new HashMap<>();
		map.put(c.getSimpleName(), subMap);
		return map;
	}

}
